const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    one:false,
    notice:true,
    notice_text:'请遵守平台使用规则，守护法律法规的底线，共同营造良好的网络环境',
    bannerList: [],
    bannerTotal:0,
    hotList:[],
    hotTotal:0,
    showOverlay:false,
    shopList:[],
    shopTotal:0,
    imageUrl:"",
    // 存放帖子的数组
    schoolList:[],
    // 访问数据库的页表结构数目，不可删除
    page:1,
    pageSize:6,
    // 跳转帖子详情用
    a:{},
    // 帖子总数目，不可删除
    count:0,
    listId:"",
    // 标签序号
    active: 0,
    value:''
  },

  gotoShop(e){
    wx.navigateTo({
      url: '/pages/shop/shop?uid='+e.currentTarget.dataset.uid,
    })
  },

  onSearch(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/search/search?shopName=' + e.detail,
    })
  },
  onCancel(){
    console.log("取消");
  },

  gotoBanner(){
    
  },

  getBanner(){
    var that = this;
    wx.request({
      url: httpUrl + '/banner',
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            bannerList:res.data.data.rows,
            bannerTotal:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  // 放大图片
  showOverlay(e){
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: e.currentTarget.dataset.listid.images
    });
    this.watch(e);
  },

  //    回到顶部
  packToTop(){
    console.log("回到顶部");
      wx.pageScrollTo({
        scrollTop:0,
          duration:300
      })
  },

// 跳转热门列表
  gotoHot(e){
    wx.navigateTo({
      url: '/pages/hot/hot?uid=' + e.currentTarget.dataset.uid,
    })
  },
  //获取热门
  getHot(){
    var that = this;
    wx.request({
      url: httpUrl + '/hot',
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            hotList:res.data.data.rows,
            hotTotal:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  getShopList(){
    var that = this;
    wx.request({
      url: httpUrl + '/shop?page='+that.data.page + '&size='+that.data.pageSize,
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            shopList:[...that.data.shopList,...res.data.data.rows],
            shopTotal:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  addCart(e){
    var that = this;
    wx.request({
      url: httpUrl + '/cart',
      method:'POST',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        shopId:e.currentTarget.dataset.uid
      },
      success(res){
        if (res.data.code == 20011) {
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:500
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('Shop_token')) {
      this.getHot();
      this.getBanner();
      this.getShopList();
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      shopList:[],
      page:1,
      shopTotal:0,
      active:0
    },()=>{
      this.getHot();
      this.getBanner();
      this.getShopList();
      wx.stopPullDownRefresh();;
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page * this.data.pageSize >= this.data.shopTotal){
      return wx.showToast({
        title: '我也是有底线的哦^.^',
        icon:'none',
      })
    }
    this.setData({
      page:this.data.page + 1,
    },()=>{
      this.getShopList();
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})